/// <summary>-----------------------------------------------------------------------///
/// Pool所有的Object																	///
///	wng0@sina.com
///	Fallout Project	
/// 2020-2-15 完成了spawn武器，运行顺利，还是很高兴的。
/// 2020-10-5 移植到新的项目RubbishGame
/// </summary>----------------------------------------------------------------------///
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PoolManager : MonoBehaviour {
	public static PoolManager Instance;
	void Awake()	
	{		
		Instance = this;	
	}
	/// <summary>-----------------------------------------------------------------	///
	/// 核心SpawnManager功能，临时保存销毁对象，降低DrawCall，降低内存消耗，提升计算速度		///
	/// </summary>----------------------------------------------------------------	///
	private List<GameObject> dormantObjects=new List<GameObject>();
	private int Capacity = 100;
	public GameObject Spawn(GameObject go)
	{
		GameObject temp = null;
		if (dormantObjects.Count > 0) {
			foreach (GameObject dob in dormantObjects) {
				if (dob.name == go.name) {
					temp = dob;
					dormantObjects.Remove (temp);
					temp.SetActive (true);
					return temp;
				}
			}
		}
		temp = GameObject.Instantiate (go) as GameObject;
		temp.name = go.name;
		return temp;
	}
	public void Despawn(GameObject go)
	{
		go.transform.SetParent(PoolManager.Instance.transform);
		go.SetActive (false);
		dormantObjects.Add (go);
		Trim ();		
	}
	public void Trim()
	{
		while (dormantObjects.Count > Capacity) {
			GameObject dob = dormantObjects [0];
			dormantObjects.RemoveAt (0);
			Destroy (dob);
		}
	}
	/// <summary>-----------------------------------------------------------------	///
	/// 核心SpawnManager功能结束，临时保存销毁对象，降低DrawCall，降低内存消耗，提升计算速度	///
	/// </summary>----------------------------------------------------------------	///


	/// <summary>-------------------------------------------------------------------///
	/// 具体需要spawn的对象列表等等。													///
	/// </summary>------------------------------------------------------------------///
	GameObject 			m_RiderPosition;
	GameObject			m_WalkerPosition;
	GameObject			m_ArrowPosition;
	public GameObject 	m_TargetPosition;
	public GameObject			m_Walker;
	public GameObject			m_Rider;
	public GameObject			m_Arrow;
	public List<GameObject>		m_WalkerList;
	public List<GameObject>		m_RiderList;
	public List<GameObject>		m_ArrowList;
	public GameObject			m_Flag;

	/*	
	public Player 				m_player;
	public string 				sceneName;
	public List<Transform> m_teacherSpawnPositionList;
	public Transform m_playerStartPosition;
	public List<Enemy> m_enemyList;
	public List<NPCTeacher> m_teacherList;
	*/
	void Start()
	{	
		DelegateManager.Instance.AfterSceneLoad += SpawnScene;						//所有的spawn动作都在场景loading之后执行。
	}
	void SpawnScene(string sceneName)
	{
		if (sceneName == "TitleScene")
		{
			return;
		}
		SpawnArrow (20);		
		SpawnRider();
		SpawnWalker();
	}
	public void SpawnArrow(int j)
	{
		m_ArrowList.Clear();
		m_ArrowPosition = GameObject.Find ("ArrowPosition");
		for (int i=0; i<j; i++)
		{
			GameObject go = Spawn(m_Arrow);
			go.transform.position=m_ArrowPosition.transform.position;
			m_ArrowList.Add(go);
		}
	}
	void SpawnRider()
	{
		m_RiderPosition = GameObject.Find ("RiderPosition");

		for (int i=0; i<10; i++)
		{
			GameObject go = Spawn(m_Rider);
			go.transform.position=m_RiderPosition.transform.position;
		}
	}
	void SpawnWalker()
	{
		m_WalkerPosition = GameObject.Find ("WalkerPosition");
		m_TargetPosition=GameObject.Find("TargetPosition");
		m_Flag=GameObject.Find("Flag");
		m_Flag.transform.position=m_WalkerPosition.transform.position;

		for (int i=0; i<10; i++)
		{
			GameObject go = Spawn(m_Walker);
			go.transform.position=m_WalkerPosition.transform.position;
		}
	}
	/*
	void SpawnTeacher()
	{
		for (int i=0; i<m_teacherSpawnPositionList.Count;i++)
		{
			for (int j=0; j<m_teacherList.Count;j++)
			{
				//GameObject go = Spawn (m_enemyList [j]);
				//Enemy enemy = (Enemy)go.GetComponent<Enemy> ();
				NPCTeacher teacher=Spawn(m_teacherList[j].gameObject).GetComponent<NPCTeacher>();
				teacher.transform.position=m_teacherSpawnPositionList[i].transform.position;
				teacher.SetUpSkills(j);
			}
		}
	}
	
	private void SpawnPlayer()
	{
		m_player= Spawn (m_playerGameObject).GetComponent < Player> ();
		GameObject playerPosition=GameObject.FindGameObjectWithTag ("PlayerPosition");
		m_player.transform.position = playerPosition.transform.position;
		//m_player.transform.position = Vector3.zero;
		m_player.SetUpPlayer ();
		//SetUpPlayersBag ();
		//ManuManager.Instance.m_UI.m_lifeBar.maxValue = m_player.m_life;
		//ManuManager.Instance.m_UI.m_maxAmmo.text = m_player.m_ammoMax.ToString();
	}
	
	
	
	void SetUpPlayersBag()
	{
		if (SaveManager.Instance.m_bag == null) {
			return;
		}
		for (int i=0;i<SaveManager.Instance.m_bag.m_assembleList.Count;i++)
		{
			m_player.m_bag.m_assembleList.Add (SaveManager.Instance.m_bag.m_assembleList [i]);
		}
		for (int i = 0; i < SaveManager.Instance.m_bag.m_itemList.Count; i++) 
		{
			m_player.m_bag.m_itemList.Add(SaveManager.Instance.m_bag.m_itemList[i]);
			m_player.m_bag.m_itemAmountList.Add(SaveManager.Instance.m_bag.m_itemAmountList[i]);
		}
	}
	
	/// <summary>----------------------------------------------------------------------------------------------------------------	///
	/// Spawns the enemy.																											///
	/// </summary>---------------------------------------------------------------------------------------------------------------	///
	void SpawnEnemy()
	{
		//m_enemySpawnPositionList = new List<Transform> ();
		GameObject[] positionArray = GameObject.FindGameObjectsWithTag ("Respawn");

		//Debug.Log (ConfigManager.Instance.m_enemyConfigList.Count);
		for (int i = 0; i < positionArray.Length; i++) 
		{
			if (i>ConfigManager.Instance.m_enemyConfigList.Count) {
				return;
			}
				GameObject go = Spawn (m_enemy);
			go.transform.position = positionArray [i].transform.position;
			go.GetComponent<Enemy> ().SetUpEnemy (i);
				//go.GetComponent<Box> ().SetUpItemList (ConfigManager.Instance.m_enemyConfigList [i].m_itemIDList, ConfigManager.Instance.m_enemyConfigList [i].m_amountList);
			//Debug.Log (ConfigManager.Instance.m_enemyConfigList [i].nameString+ go.transform.position.ToString());
			}
		}

	
	void SpawnEnemy()
	{
		for (int i=0; i<m_enemySpawnPositionList.Count;i++)
		{
			for (int j=0; j<m_enemyList.Count;j++)
			{
				//GameObject go = Spawn (m_enemyList [j]);
				//Enemy enemy = (Enemy)go.GetComponent<Enemy> ();
				Enemy enemy=Spawn(m_enemyList[j].gameObject).GetComponent<Enemy>();
				enemy.transform.position=m_enemySpawnPositionList[i].transform.position;
				enemy.SetUpEnemy(j);
			}
		}
	}
	/// <summary>
	/// Spawns the weapon.
	/// </summary>
	/// <param name="weapon">Weapon. if there is no weapon, then input null</param>
	public void SpawnWeapon(Weapon weapon)
	{	
		if (m_currentWeapon!=null)
		{
			Despawn(m_currentWeapon);
		}	
		if(weapon==null)
		{
			GameObject go=Resources.Load<GameObject>("Hand");
			m_currentWeapon=Spawn(go);
		}
		if (weapon!=null)
		{
			GameObject go =Resources.Load<GameObject>(weapon.modelAddress);
			m_currentWeapon=Spawn(go);
			Debug.Log(weapon.modelAddress);
		}
		m_currentWeapon.transform.SetParent(m_WeaponPosition.transform);
		m_currentWeapon.transform.localPosition=Vector3.zero;
		m_currentWeapon.transform.localEulerAngles=Vector3.zero;

	}
	*/
}